package com.tfjybj.itoo.teach.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.RepairEntity;
import com.tfjybj.itoo.teach.model.RepairModel;
import com.tfjybj.itoo.teach.provider.service.RepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * RepairController
 * repair表
 *
 * @author 马莹
 * @version ${version}
 * @since ${version} 2019-05-22 21:02:48
 */
@Api(tags = {"repair表接口"})
@RequestMapping(value = "/repair")
@RestController
public class RepairController {

    @Resource
    private RepairService repairService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model RepairModel
     * @return 添加的结果
     * @author 马莹
     * @since ${version} 2019-05-22 21:02:48
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody RepairModel model) {
        if (StringUtils.isEmpty(model.getRepairStatus())) {
            return ItooResult.build(ItooResult.FAIL, "repairStatus为空");
        }
        if (StringUtils.isEmpty(model.getRequestId())) {
            return ItooResult.build(ItooResult.FAIL, "requestId为空");
        }
        RepairEntity repairEntity = new RepairEntity();
        BeanUtils.copyProperties(model, repairEntity);
        repairService.save(repairEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 测试
     */


    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 马莹
     * @since ${version} 2019-05-22 21:02:48
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        repairService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 马莹
     * @since ${version} 2019-05-22 21:02:48
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        repairService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model RepairModel
     * @return 修改后的结果
     * @author 马莹
     * @since ${version} 2019-05-22 21:02:48
     */
    @ApiOperation(value = "根据id修改repair")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody RepairModel model) {
        if (StringUtils.isEmpty(model.getRepairStatus())) {
            return ItooResult.build(ItooResult.FAIL, "repairStatus为空");
        }
        if (StringUtils.isEmpty(model.getRequestId())) {
            return ItooResult.build(ItooResult.FAIL, "requestId为空");
        }
        RepairEntity repairEntity = new RepairEntity();
        BeanUtils.copyProperties(model, repairEntity);
        repairService.updateById(repairEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Repair
     *
     * @param id 主键id
     * @return 根据id查找的结果
     * @author 马莹
     * @since ${version} 2019-05-22 21:02:48
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        RepairEntity repairEntity = repairService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", repairEntity);
    }

    /**
     * 分页查询所有Repair
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 马莹
     * @since ${version} 2019-05-22 21:02:48
     */
    @ApiOperation(value = "分页查询所有Repair")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<RepairEntity> repairs = repairService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", repairs);
    }

    //endregion

    /* **********************************以下为非模板生成的内容********************************* */


    /**
     * 根据登录人id查询报修情况
     *
     * @param requestId 登录人id
     * @return 返回实体集合
     * @author 马莹
     * @since 2019-5-23 11:56:04
     */
    @ApiOperation(value = "根据登录人查询报修状态")
    @GetMapping(value = "/queryUnderRepair/{requestId})")
    public ItooResult queryUnderRepair(@PathVariable String requestId) {
        return ItooResult.build(ItooResult.SUCCESS, "查询成功!", repairService.queryUnderRepair(requestId));
    }
}    
